package com.anhry.app.safety.manage.djpd.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.anhry.app.safety.manage.djpd.bean.TbDjpdApplyWs1;
import com.anhry.app.safety.manage.djpd.bean.TbDjpdApplyWs2;
import com.anhry.app.safety.manage.djpd.bean.TbDjpdHyCheck;
import com.anhry.app.safety.manage.djpd.bean.TbDjpdType;
import com.anhry.app.safety.manage.djpd.service.DjpdService;
import com.anhry.app.safety.util.dao.UtilDao;

/**
 * @date:2012-11-1
 * @remark:
 * @author:dongbin
 */

@Service
@SuppressWarnings("unchecked")
public class DjpdServiceImpl implements DjpdService{

	private static Logger logger = Logger.getLogger(DjpdServiceImpl.class);
	@Resource
	private UtilDao utilDao;
	
	@Resource
	private JdbcTemplate jdbcTemplate;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}
	
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public TbDjpdType getBzhType(String str) throws Exception {
		List list = (List)this.utilDao.getHqlQuery("from TbDjpdType t where 1=1 and t.typeName='"+str+"'");
		if(null!=list && list.size()>0){
			return (TbDjpdType)list.get(0);
		}
		return null;
	}
	
	public TbDjpdApplyWs1 getDjpdWs1(String appId) throws Exception {
		List list = (List)this.utilDao.getHqlQuery("from TbDjpdApplyWs1 t where 1=1 and t.applyId="+appId);
		if(null!=list && list.size()>0){
			return (TbDjpdApplyWs1)list.get(0);
		}
		return null;
	}

	public TbDjpdApplyWs2 getDjpdWs2(String appId) throws Exception {
		List list = (List)this.utilDao.getHqlQuery("from TbDjpdApplyWs2 t where 1=1 and t.applyId="+appId);
		if(null!=list && list.size()>0){
			return (TbDjpdApplyWs2)list.get(0);
		}
		return null;
	}

	public TbDjpdHyCheck getHyCheck(String appId) throws Exception {
		List list = (List)this.utilDao.getHqlQuery("from TbDjpdHyCheck t where 1=1 and t.applyId="+appId);
		if(null!=list && list.size()>0){
			return (TbDjpdHyCheck)list.get(0);
		}
		return null;
	}

	public int deleteApply(String corpId) throws Exception {
		if(StringUtils.isNotBlank(corpId)) {
			logger.info("delete djpd apply for corp with ID -----> " + corpId);
			return jdbcTemplate.update("delete from tb_djpd_apply where corp_id = " + corpId);
		}
		return 1;
	}

	public int deleteApplyItem(String corpId) throws Exception {
		if(StringUtils.isNotBlank(corpId)) {
			logger.info("delete djpd apply items for corp with ID -----> " + corpId);
			return jdbcTemplate.update("delete from tb_djpd_apply_item where corp_id = " + corpId);
		}
		return 1;
	}

	public int deleteApplyWs1(String applyId) throws Exception {
		if(StringUtils.isNotBlank(applyId)) {
			logger.info("delete tb_djpd_apply_ws1 for applyId -----> " + applyId);
			return jdbcTemplate.update("delete from tb_djpd_apply_ws1 where apply_id = " + applyId);
		}
		return 1;
	}

	public int deleteApplyWs2(String applyId) throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotBlank(applyId)) {
			logger.info("delete tb_djpd_apply_ws2 for applyId -----> " + applyId);
			return jdbcTemplate.update("delete from tb_djpd_apply_ws2 where apply_id = " + applyId);
		}
		return 1;
	}
}
